package com.sudichina.sudichina.view.activity.releaseline;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.adapter.BaseListViewViewHolder;
import com.sudichina.sudichina.adapter.MySuperListViewAdapter;
import com.sudichina.sudichina.bean.AddCityWeightBean;
import com.sudichina.sudichina.bean.CityPrice;
import com.sudichina.sudichina.bean.TTranLineSegmentVO;
import com.sudichina.sudichina.bean.TTranLineSeviceVO;
import com.sudichina.sudichina.entity.ReleaseRoute;
import com.sudichina.sudichina.querydatabase.DBManager;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.LineManageActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;
import com.sudichina.sudichina.view.customview.xlistview.NoscrollListview;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${杨伟乔} on 2017/3/10.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */
public class LCLPriceActivity extends
        BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener, onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.listview_city_tj)
    NoscrollListview listviewCityTj;
    @BindView(R.id.tv_add_way_city)
    TextView tvAddWayCity;
    @BindView(R.id.btn_fabu)
    Button privateBtnFabu;


    private String destination;

    ArrayList<CityPrice> citys = new ArrayList<>();
    private MySuperListViewAdapter cityAdapter;

    //斤方吨用到的变量
    private final int DIALOG_JIN = 1001;
    private final int DIALOG_FANG = 1002;
    private final int DIALOG_DUN = 1003;
    private int DIALOG_CURRENTPOSITION = DIALOG_JIN;
    private String STR_DIALOG_JIN = "";
    private String STR_DIALOG_FANG = "";
    private String STR_DIALOG_DUN = "";


    private TextView tvJin;
    private TextView tvFang;
    private TextView tvDun;
    private EditText etPrice;
    private boolean clearAvidListern = false;
    private AlertDialog chooseWeight;

    private int currentPosition = -1;
    private TextView tvTjCity;
    private String cityName;
    private AddCityWeightBean picked_city;
    private TextView tvTjWeight;
    ArrayList<AddCityWeightBean> list = new ArrayList();
    private ReleaseRoute releaseRoute;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_lcl);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        releaseRoute = (ReleaseRoute) intent.getSerializableExtra("releaseRoute");


        tvTitle.setText("报价");

        initData();


        NoscrollListview listview_city_tj = (NoscrollListview) findViewById(R.id.listview_city_tj);

        cityAdapter = new MySuperListViewAdapter(citys) {
            @Override
            public BaseListViewViewHolder setHolder() {
                class Myviewholder extends BaseListViewViewHolder {


                    @Override
                    public void setDatas(View view, Object obj, int position) {
                        ImageView imgTj = (ImageView) view.findViewById(R.id.img_tj);
                        TextView tvTjCity = (TextView) view.findViewById(R.id.tv_tjcity);

                        TextView tvkg = (TextView) view.findViewById(R.id.tv_kg);
                        TextView tvfang = (TextView) view.findViewById(R.id.tv_fang);
                        TextView tvdun = (TextView) view.findViewById(R.id.tv_dun);
                        TextView tvall = (TextView) view.findViewById(R.id.tv_all);

                        CityPrice cityPrice = citys.get(position);
                        String name = cityPrice.getName();


                        if (position == 0)

                        {
//                            表示出发地
                            imgTj.setImageResource(R.mipmap.shang);

                        } else if (position == citys.size() - 1)

                        {
                            //表示目的地
                            imgTj.setImageResource(R.mipmap.xia);
                            tvTjCity.setText(name);//设置城市名
                            tvkg.setText(cityPrice.getJinPrice());
                            tvfang.setText(cityPrice.getFangPrice());
                            tvdun.setText(cityPrice.getDunPrice());
                            tvall.setText(cityPrice.getAllPrice());
                        } else

                        {
                            //中间的地方
                            imgTj.setImageResource(R.mipmap.zhong);
                            tvTjCity.setText(name);//设置城市名
                            tvkg.setText(cityPrice.getJinPrice());
                            tvfang.setText(cityPrice.getFangPrice());
                            tvdun.setText(cityPrice.getDunPrice());
                            tvall.setText(cityPrice.getAllPrice());
                        }


                    }

                    @Override
                    public int getItemId() {
                        return R.layout.item_city_tj;
                    }
                }


                return new

                        Myviewholder();
            }
        }

        ;
        listview_city_tj.setAdapter(cityAdapter);
        listview_city_tj.setOnItemClickListener(this);

    }


    private void initData() {

        citys.add(new CityPrice(releaseRoute.getFromAddress()));


        citys.add(MyApp.getCity());


    }


    @OnClick({R.id.iv_back, R.id.tv_add_way_city, R.id.btn_fabu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_way_city:


                Intent intent = new Intent(this, CheckAddressActivity.class);

                startActivityForResult(intent, Contains.ADD_WAY_CITY);


                break;

            case R.id.btn_fabu:

                releaseRoute.setAccountId(String.valueOf(MyApp.getCurrentUser().getId()));
                releaseRoute.setType(null);
                TTranLineSeviceVO tTranLineSeviceVO = new TTranLineSeviceVO();


                tTranLineSeviceVO.setServiceType(String.valueOf(1));

                tTranLineSeviceVO.setServicePrice("500");


                ArrayList<TTranLineSeviceVO> list1 = new ArrayList<TTranLineSeviceVO>();
                list1.add(tTranLineSeviceVO);
                releaseRoute.setTlist(list1);
                ArrayList<TTranLineSegmentVO> tTranLineSegmentVOs = new ArrayList<>();
                //添加途径城市
                // TODO: 2017/3/23
//                list
                for (AddCityWeightBean city :
                        list) {

                    tTranLineSegmentVOs.add(new TTranLineSegmentVO());
                }


                releaseRoute.setTtLinelist(tTranLineSegmentVOs);

                NetUtils.requestDataformentity(Api.RELEASELINEASAPP, releaseRoute, this, null);


                break;

        }
    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {

        String bean1 = (String) bean;

        if (bean1.contains("成功")) {
            Toastutils.showToast("路线发布成功");

            startActivity(new Intent(this, LineManageActivity.class));
            finish();
        } else {
            Toastutils.showToast("路线发布失败,已有发布的行程");
        }


    }

    @Override
    public void requestdataFailed(IOException e) {

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Contains.ADD_WAY_CITY:
                CityPrice cityPrice = (CityPrice) data.getSerializableExtra("cityPrice");

                if (cityPrice != null) {
                    for (CityPrice city : citys) {
                        if (city.getName().equals(cityPrice.getName())) {
                            Toastutils.showToast("请勿重复添加!!");
                            return;
                        }
                    }

                    citys.add(citys.size()-1,cityPrice);
                    cityAdapter.notifyDataSetChanged();
                }

                break;

        }


    }
}